<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:11pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:xx-large; color:#000000;">Как сжать изображение в Python</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><a href="https://pythonist.ru/category/topnews/"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:12px; font-weight:600; color:#03c4eb; background-color:transparent; text-transform:uppercase;">ТОП</span></a></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px; background-color:#ffffff;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Сегодня мы поговорим про то, как сжать изображение в Python. Вы узнаете, как уменьшить размер файла, сжимая и изменяя размер изображения с помощью библиотеки Pillow.</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Сжатие изображения — это процесс уменьшения веса картинки без ухудшения ее качества. Есть много онлайн-инструментов, которые предлагают эту услугу. Большинство из них являются отличным вариантом, если вы хотите быстро и надежно уменьшить вес изображения. Но не всегда это лучшее решение. Поэтому в этой статье мы расскажем, как уменьшать размер файла изображения в Python с помощью библиотеки Pillow.</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Кроме того, вы можете свободно использовать код из этого руководства. Например, вы можете создать вокруг него API для пакетного уменьшения размеров изображений вместо использования стороннего API, который будет стоить вам денег.</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Мы сделали код для данного урока максимально гибким. Вы можете сжать изображение и изменить его размер с коэффициентом масштабирования или точной шириной и высотой. Более того, вы также можете указать желаемое качество.</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:x-large; color:#000000;">Начало работы</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Приступим! Для начала нужно установить </span><a href="https://pillow.readthedocs.io/en/stable/"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#03c4eb; background-color:transparent;">библиотеку Pillow</span></a><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">$ pip install Pillow</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Теперь откройте новый файл Python и </span><a href="https://pythonist.ru/kak-importirovat-v-python/"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#03c4eb; background-color:transparent;">импортируйте</span></a><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;"> нашу библиотеку следующим образом:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">import</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> os</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">from </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; font-style:italic; color:#4a9141;">PIL</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;"> import</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Прежде чем углубиться в сжатие изображений, давайте напишем функцию для получения размера файла в удобном для нас формате:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">def</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">get_size_format</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">b, factor=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">1024</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, suffix=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;B&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; font-style:italic; color:#dd1144;">&quot;&quot;&quot;</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; font-style:italic; color:#dd1144;">    Scale bytes to its proper byte format</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; font-style:italic; color:#dd1144;">    e.g:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; font-style:italic; color:#dd1144;">        1253656 =&gt; '1.20MB'</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; font-style:italic; color:#dd1144;">        1253656678 =&gt; '1.17GB'</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; font-style:italic; color:#dd1144;">    &quot;&quot;&quot;</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">for</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> unit </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">in</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;K&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;M&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;G&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;T&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;P&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;E&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;Z&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">if</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> b </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">&lt;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> factor:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">            </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">return</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> f</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;{b:.2f}{unit}{suffix}&quot;</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        b /= factor</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">return</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> f</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;{b:.2f}Y{suffix}&quot;</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">А теперь создадим нашу основную функцию для сжатия изображений. Выглядеть она будет следующим образом:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">def</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">compress_img</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">image_name, new_size_ratio=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">0.9</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, quality=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">90</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, width=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#34068a;">None</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, height=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#34068a;">None</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, to_jpg=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#34068a;">True</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># load the image to memory</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    img = Image.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">open</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">image_name</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># print the original image shape</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">print</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;[*] Image shape:&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, img.size</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># get the original image size in bytes</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    image_size = os.path.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">getsize</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">image_name</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># print the size before compression/resizing</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">print</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;[*] Size before compression:&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">get_size_format</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">image_size</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">))</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">if</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> new_size_ratio </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">&lt;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">1.0</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># if resizing ratio is below 1.0, then multiply width &amp; height with this ratio to reduce image size</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        img = img.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">resize</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">((</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">int</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">img.size</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">0</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> * new_size_ratio</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">int</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">img.size</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">1</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> * new_size_ratio</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">))</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, Image.ANTIALIAS</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># print new image shape</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">print</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;[+] New Image shape:&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, img.size</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">elif</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> width </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#4da0d2; background-color:#f0f0f0;">and</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> height:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># if width and height are set, resize with them instead</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        img = img.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">resize</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">((</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">width, height</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, Image.ANTIALIAS</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># print new image shape</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">print</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;[+] New Image shape:&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, img.size</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># split the filename and extension</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    filename, ext = os.path.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">splitext</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">image_name</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># make new filename appending _compressed to the original file name</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">if</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> to_jpg:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># change the extension to JPEG</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        new_filename = f</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;{filename}_compressed.jpg&quot;</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">else</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># retain the same extension of the original image</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        new_filename = f</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;{filename}_compressed{ext}&quot;</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">try</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># save the image with the corresponding quality and optimize set to True</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        img.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">save</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">new_filename, quality=quality, optimize=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#34068a;">True</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">except</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> OSError:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># convert the image to RGB mode first</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        img = img.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">convert</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;RGB&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># save the image with the corresponding quality and optimize set to True</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        img.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">save</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">new_filename, quality=quality, optimize=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#34068a;">True</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">print</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;[+] New file saved:&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, new_filename</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># get the new image size in bytes</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    new_image_size = os.path.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">getsize</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">new_filename</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># print the new size in a good format</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">print</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;[+] Size after compression:&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">get_size_format</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">new_image_size</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">))</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># calculate the saving bytes</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    saving_diff = new_image_size - image_size</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># print the saving percentage</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">print</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">f</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;[+] Image size change: {saving_diff/image_size*100:.2f}% of the original image size.&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Какая-то гигантская функция, которая делает много всего. Жуть, не правда ли? Но не переживайте. Сейчас мы рассмотрим ее подробнее и всё станет понятно. </span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#777777;">[python_ad_block]</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Итак:</span></p>
<ol style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;"><li style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;" style=" margin-top:20px; margin-bottom:6px; margin-left:69px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:16px;">Мы используем метод </span><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">Image.open()</span><span style=" font-size:16px;"> для загрузки изображения в память. Далее мы получаем размер файла изображения с помощью функции </span><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">os.path.getsize()</span><span style=" font-size:16px;">. Мы делаем это для того, чтобы позже иметь возможность сравнить этот размер с размером нового сгенерированного файла.</span></li>
<li style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;" style=" margin-top:8px; margin-bottom:6px; margin-left:69px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:16px;">Если значение </span><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">new_size_ratio</span><span style=" font-size:16px;"> меньше </span><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">1.0</span><span style=" font-size:16px;">, то необходимо изменить размер. Это число находится в диапазоне от 0 до 1 и умножается на ширину и высоту исходного изображения, чтобы получить изображение с более низким разрешением. Это подходящий параметр, если вы хотите еще больше уменьшить размер изображения. Вы также можете установить его на 0,95 или 0,9, чтобы уменьшить размер изображения с минимальными изменениями разрешения.</span></li>
<li style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;" style=" margin-top:8px; margin-bottom:6px; margin-left:69px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:16px;">Если значение </span><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">new_size_ratio</span><span style=" font-size:16px;"> равно </span><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">1.0</span><span style=" font-size:16px;">, но заданы ширина и высота, мы изменяем размер до этих новых значений ширины и высоты. Убедитесь, что они меньше исходной ширины и высоты!</span></li>
<li style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;" style=" margin-top:8px; margin-bottom:6px; margin-left:69px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:16px;">Если для </span><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">to_jpg</span><span style=" font-size:16px;"> установлено значение </span><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">True</span><span style=" font-size:16px;">, мы меняем расширение исходного изображения на JPEG. Это значительно уменьшит размер изображения, особенно для изображений PNG. Если возникнет ошибка </span><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">OSError</span><span style=" font-size:16px;">, преобразование формата изображения в RGB решит данную проблему.</span></li>
<li style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;" style=" margin-top:8px; margin-bottom:20px; margin-left:69px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:16px;">И наконец, мы используем метод </span><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">save()</span><span style=" font-size:16px;"> для записи оптимизированного изображения. Мы передаем в качестве параметров имя нового файла, желаемое качество и optimize со значением </span><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">True</span><span style=" font-size:16px;">. После этого мы получаем размер нового изображения. Его мы сравниваем с размером исходного изображения, который получили в самом начале.</span></li></ol>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:x-large; color:#000000;">Сжатие изображений на примерах</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Теперь, когда у нас есть основная функция, давайте воспользуемся модулем </span><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">argparse</span><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">, чтобы интегрировать ее с аргументами командной строки. Сделаем это следующим образом:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">if</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">__name__</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> == </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;__main__&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">import</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> argparse</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    parser = argparse.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">ArgumentParser</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">description=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;Simple Python script for compressing and resizing images&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    parser.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">add_argument</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;image&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, help=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;Target image to compress and/or resize&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    parser.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">add_argument</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;-j&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;--to-jpg&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, action=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;store_true&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, help=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;Whether to convert the image to the JPEG format&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    parser.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">add_argument</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;-q&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;--quality&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, type=int, help=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;Quality ranging from a minimum of 0 (worst) to a maximum of 95 (best). Default is 90&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, default=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">90</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    parser.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">add_argument</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;-r&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;--resize-ratio&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, type=float, help=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;Resizing ratio from 0 to 1, setting to 0.5 will multiply width &amp; height of the image by 0.5. Default is 1.0&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, default=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">1.0</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    parser.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">add_argument</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;-w&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;--width&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, type=int, help=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;The new width image, make sure to set it with the `height` parameter&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    parser.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">add_argument</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;-hh&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;--height&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, type=int, help=</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;The new height for the image, make sure to set it with the `width` parameter&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    args = parser.</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">parse_args</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">()</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># print the passed arguments</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">print</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;=&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">50</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">print</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;[*] Image:&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, args.image</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">print</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;[*] To JPEG:&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, args.to_jpg</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">print</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;[*] Quality:&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, args.quality</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">print</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;[*] Resizing ratio:&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, args.resize_ratio</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#286491;">if</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> args.width </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#4da0d2; background-color:#f0f0f0;">and</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> args.height:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">print</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;[*] Width:&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, args.width</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">        </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">print</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;[*] Height:&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, args.height</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">print</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#dd1144;">&quot;=&quot;</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">50</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#8181a1;"># compress the image</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">    </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#0086b3;">compress_img</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">args.image, args.resize_ratio, args.quality, args.width, args.height, args.to_jpg</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Здесь мы создали парсер аргументов командной строки и добавили параметры, которые уже обсуждали выше.</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Теперь воспользуемся нашим скриптом. Вы можете получить пример изображения по </span><a href="https://github.com/x4nth055/pythoncode-tutorials/blob/master/python-for-multimedia/compress-image/sample-satellite-images.png"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#03c4eb; background-color:transparent;">этой ссылке</span></a><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">.</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Для начала применим наш скрипт без каких-либо параметров. Это будет выглядеть так:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">$ python compress_image.py sample-satellite-images.png</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">И вот какой результат мы получим:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">==================================================</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image: sample-satellite-images.png</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> To JPEG: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#34068a;">False</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Quality: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">90</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Resizing ratio: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">1.0</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">==================================================</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image shape: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">953</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">496</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Size before compression: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">425.65</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">KB</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> New file saved: sample-satellite-images_compressed.png</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Size after compression: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">379.25</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">KB</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image size change: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">-10.90</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">% of the original image size.</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Вес изображения уменьшен с 425,65 КБ до 379,25 КБ. Это примерно 11%. Не много, но всё-таки. Далее попробуем передать </span><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">-j</span><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;"> для преобразования нашего изображения из формата PNG в JPEG:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">$ python compress_image.py sample-satellite-images.png -j</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Запустим наш код и получим следующий результат:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">==================================================</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image: sample-satellite-images.png</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> To JPEG: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#34068a;">True</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Quality: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">90</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Resizing ratio: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">1.0</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">==================================================</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image shape: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">953</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">496</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Size before compression: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">425.65</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">KB</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> New file saved: sample-satellite-images_compressed.jpg</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Size after compression: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">100.07</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">KB</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image size change: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">-76.49</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">% of the original image size.</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; font-style:italic; color:#242424;">Примечание.</span><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;"> Получить образец данного изображения вы можете </span><a href="https://github.com/x4nth055/pythoncode-tutorials/blob/master/python-for-multimedia/compress-image/sample-satellite-images.png"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#03c4eb; background-color:transparent;">здесь</span></a><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">.</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Это просто фантастика! Как мы видим, вес уменьшился на 76,5%! Теперь немного уменьшим качество с помощью такой команды:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">$ python compress_image.py sample-satellite-images.png -j -q 75</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">И получим следующий вывод:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">==================================================</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image: sample-satellite-images.png</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> To JPEG: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#34068a;">True</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Quality: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">75</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Resizing ratio: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">1.0</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">==================================================</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image shape: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">953</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">496</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Size before compression: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">425.65</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">KB</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> New file saved: sample-satellite-images_compressed.jpg</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Size after compression: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">64.95</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">KB</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image size change: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">-84.74</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">% of the original image size.</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Около 85% уменьшения файла без изменения исходного разрешения изображения! Впечатляет, не правда ли? Что ж, давайте попробуем умножить ширину и высоту изображения на 0,9:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">$ python compress_image.py sample-satellite-images.png -j -q 75 -r 0.9</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">И вот что у нас получится:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">==================================================</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image: sample-satellite-images.png</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> To JPEG: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#34068a;">True</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Quality: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">75</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Resizing ratio: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">0.9</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">==================================================</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image shape: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">953</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">496</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Size before compression: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">425.65</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">KB</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> New Image shape: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">857</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">446</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> New file saved: sample-satellite-images_compressed.jpg</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Size after compression: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">56.94</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">KB</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image size change: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">-86.62</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">% of the original image size.</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Теперь последний штрих. Давайте установим точные значения ширины и высоты:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Monaco,Consolas,Andale Mono,DejaVu Sans Mono,monospace'; font-size:16px; color:#bd4147; background-color:#e9e9e9;">$ python compress_image.py sample-satellite-images.png -j -q 75 -w 800 -hh 400</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Запустим и получим такой результат:</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">==================================================</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image: sample-satellite-images.png</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> To JPEG: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#34068a;">True</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Quality: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">75</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Resizing ratio: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">1.0</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Width: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">800</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Height: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">400</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">==================================================</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image shape: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">953</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">496</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">*</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Size before compression: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">425.65</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">KB</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> New Image shape: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">(</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">800</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">, </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">400</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">)</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> New file saved: sample-satellite-images_compressed.jpg</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Size after compression: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">49.73</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">KB</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#f7f7fa;"><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">[</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">+</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#777777;">]</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;"> Image size change: </span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#009999;">-88.32</span><span style=" font-family:'inherit'; font-size:15px; font-weight:696; color:#2f3235;">% of the original image size.</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Потрясающе! Размер изображения уменьшился на 88%! Это прекрасный результат!</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:x-large; color:#000000;">Заключение</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Итак, теперь вы знаете, как сжать изображение в Python! Мы рассмотрели примеры и смогли уменьшить вес картинки на 88% без ухудшения разрешения. Невероятно! Что ж, теперь вы можете попробовать настроить параметры в соответствии с вашими потребностями. </span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Мы надеемся, что эта статья была для вас полезна, а данный скрипт будет удобным в использовании и поможет вам в ваших дальнейших проектах!</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Полную версию кода можно получить по следующей </span><a href="https://www.thepythoncode.com/code/compress-images-in-python"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#03c4eb; background-color:transparent;">ссылке</span></a><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">.</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:36px; margin-right:36px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">Перевод статьи </span><a href="https://www.thepythoncode.com/article/compress-images-in-python"><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#03c4eb; background-color:transparent;">«How to Compress Images in Python»</span></a><span style=" font-family:'-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif'; font-size:16px; color:#242424;">.</span></p></body></html>